

/**
 * 
 *  初始化方法
 */
$(function(){
	queryStrategy();
});


/**
 * 查询策略列表
 */
function queryStrategy(){
	var Strategy =[{code:'seID',name:'checkBox',width:10},
	               {code:'name',name:'策略名称'},
	               {code:'type',name:'策略类型'},
	               {code:'state',name:'状态'},
	               {code:'B_D',name:'操作'}         
	           ];	
	 var  parm={url:basePath+'/radar/strategy/getStrategyList.do',divID:'strategy',afterQuery:'queryLabel'};// 常规参数
	 var  data={"resourceId":resourceId};    // 赛选条件
     easyQuery(Strategy,parm,data);	
}



/**
 * 指标个数标签
 */
function queryLabel(){
	
    var perfNum = 0;
    var capacNum = 0;
    var configNum = 0;
    var serviceNum = 0;
    var processNum = 0;
    var stateNum = 0;
    var tempNum = 0;
    var dbNum = 0;
    var dbOracleNum = 0;
    var networkConfNum = 0;
    var networkPerfNum = 0;
    
    $("td[code='type']").each(function(){
        var typevalue=$(this).html()
        if(resourceType == "os"){
			if(typevalue=='性能'){
				perfNum++;
			}else if(typevalue=='容量'){
				capacNum++;
			}else if(typevalue=='配置'){
				configNum++;
			}else if(typevalue=='服务'){
				serviceNum++;
			}else if(typevalue=='进程'){
				processNum++;
			}else if(typevalue=='状态'){
				stateNum++;
			}else if(typevalue=='状态'){
				tempNum++;
			}
		}else if(resourceType == "db"){
			if(typevalue=='MySql'){
				dbNum++;
			}else if(typevalue=='Oracle'){
				dbOracleNum++;
			}
		}else if(resourceType == "network"){
			if(typevalue=='配置'){
				networkConfNum++;
			}else if(typevalue=='性能'){
				networkPerfNum++;
			}
		}
    });
    
		
		$("#perfNum").html("性能（"+perfNum+"）");
		$("#capacNum").html("容量（"+capacNum+"）");
		$("#configNum").html("配置（"+configNum+"）");
		$("#serviceNum").html("服务（"+serviceNum+"）");
		$("#processNum").html("进程（"+processNum+"）");
		$("#stateNum").html("状态（"+stateNum+"）");
		$("#tempNum").html("温度（"+tempNum+"）");
		$("#dbNum").html("MySql（"+dbNum+"）");
		$("#dbOracleNum").html("Oracle（"+dbOracleNum+"）");
		$("#networkConfNum").html("配置（"+networkConfNum+"）");
		$("#networkPerfNum").html("性能（"+networkPerfNum+"）");
		$("#checkAllStrategy").attr("checked",false);		
}



/**
 * 操作 设备删除按钮
 */
function del_strategy(feild) {
	var id = $(feild).parent().parent().attr("nodeId");
	delEq(id);
}
/**
 * 删除按钮
 */
function deleteCheckedStrategy() {
	var id = '';
	$("input[type='checkbox'][nodeId][ divID='strategy']:checked").each(function() {
		id = id + $(this).attr("nodeId") + ",";
	});
	if (id == '') {
		layer.msg('请选择要删除的选项', {
			icon : 1
		});
		return;
	}
	id = id.substring(0, id.length - 1);
	delEq(id);
}
/**
 * @param id
 */
function delEq(id) {
	
	layer.confirm("确定要删除该设备？", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		$.ajax({
			url : basePath + "/radar/strategy/deleteStrategyResource.do",
			type : "post",
			dataType : 'json',
			data : {
				"id" : id, // 绑定 设备的id
				"resourceType" : resourceType
			},
			success : function(result) {
				if (result.data == 'success') {
					layer.msg('删除成功！', {
						icon : 1
					});
					reQuery("strategy");
				} else {
					layer.msg('删除失败！', {
						icon : 1
					});
				}
			}
		});
	});
}

	/**
	 * 按钮执行未应用全部策略
	 */
	function appAllStrategy() {
		var  ids='';
		$("td[code='state']").each(function() {
			var   value=$(this).html();
			if(value=='未应用'||value=='应用失败'){
				ids=ids+$(this).parent().attr("nodeId")+","
			}
			
		});
		if(ids==''){
			layer.msg('没有可应用的策略！', {
				icon : 1
			});
		}else{
			ids=ids.substring(0,ids.length-1);
		}
			
		applyPolicy(ids);
	}
	
	/**
	 * 下发策略
	 * 
	 * 
	 */
	function applyPolicy(ids) {

		
		$.ajax({
			url : basePath + "/radar/strategy/applyPolicy.do",
			type : "post",
			dataType : 'json',
			data : {
				"ids" : ids, // 绑定 设备的id
				"resourceType" : resourceType
			},
			success : function(result) {
				if (result.data == 'success') {
					layer.msg('操作成功！', {
						icon : 1
					});
					reQuery("strategy");
				} else {
					layer.msg('操作失败！'+result.data, {
						icon : 1
					});
				}
			}
		});
	}

	
/**执行按钮
 * @param feild
 */
function  runexec_strategy(feild){
	var flag=false;
	var id = $(feild).parent().parent().attr("nodeId");
	$("td[code='state']").each(function() {
		var nowid  = $(this).parent().attr("nodeId");
		if(nowid==id){
			var   value=$(this).html();
			if(value=='未应用'||value=='应用失败'){
				flag=true;
			}
		}	
	});
	
	if(flag){
		applyPolicy(id);
	}else{
		layer.msg('已应用成功！不能重复应用！', {
			icon : 1	});
	}
	
}




//全选框选择
function checkStrategyAll(field){
	$(":checkbox[name='strategy']").attr("checked",field.checked);
}
//单个选择处理
function manuStrategyCheck(field){
	if(field.checked==false){
		$("#checkAllStrategy").removeAttr("checked");
	}else{
		var flagCheckAll = true;
		$("input[name='strategy']").each(function(){
			if($(this).attr("checked")!='checked'){
				flagCheckAll=false;
			} 
		});
		if(flagCheckAll){
			$("#checkAllStrategy").attr("checked","checked");
		}
	}
}
//管理策略
function managerStrategy(){
	layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:2,
        title:'添加策略',
        area:['800px','450px'],
        content:basePath+'/radar/strategy/appendStrategy.do?resourceId='+resourceId+'&resourceType='+resourceType,
        maxmin: true,
        shadeClose: true
    });
}
